package com.zhongli.devplatform.global;

import com.zhongli.devplatform.exception.ServiceException;
import com.zhongli.devplatform.bean.Res;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Res defaultErrorHandler(Exception e) {
        e.printStackTrace();
        log.error(e.getLocalizedMessage());
        if (e instanceof IllegalArgumentException) {
            return Res.error("参数异常");
        } else if (e instanceof HttpRequestMethodNotSupportedException) {
            return Res.error("访问地址不存在");
        } else if (e instanceof ServiceException) {
            return Res.error(e.getMessage());
        }
        return Res.error("系统错误，请联系管理员");
    }

}
